/**飞机类型 */
enum EnemyType { "feiji0_png", "敌机1_png", "敌机2_png", "敌机3_png", "E4_png" };

module Fighter {
	/**
	 * 飞机基础---刘伟
	 * （1）移动
	 * （2）重置
	 * （3）生成
	 * （4）发射子弹
	 * （5）死亡
	 * */
	export class BasicFly {
		/**初始化飞机 */
		public init(showPanle, flyManager, type) {
			this.flyManager = flyManager;
			this.type = type;
			this.showPanle = showPanle;
			this.getConfig();
			this.create();
			this.AnimationManager = this.showPanle.Animation;
			this.GoldJump = new Gold.GoldJump();
			this.lastTime = egret.getTimer();
		}
		/**初始速度*/
		public speed_config:number;
		/**飞机 */
		public fly: egret.Bitmap;
		/**上一帧时间*/
		public lastTime: number;
		/**飞机唯一标识 */
		public id: number;
		/**飞机类型 */
		public type: number;
		/**动画管理类*/
		public AnimationManager: Animation.AnimationManager;
		/**显示的容器*/
		public showPanle: any;
		/**飞机的管理容器 */
		public flyManager: FlyManager;
		/**飞机血量 */
		public hp: number;
		/**初始定义血量 */
		public hpDef: number;
		/**飞机血条 */
		public progress: eui.ProgressBar;
		/**飞机运行速度 */
		public speed: number;
		/**是否是远程显示敌方防守飞机 */
		public showInEnemyDefense: boolean = false;
		/**记录飞机的伤害 */
		public value: number;
		/**飞机是否显示在舞台上 */
		public isLiving: boolean = false;
		/**金币跳动*/
		public GoldJump: Gold.GoldJump;
		/**阿尔法值*/
		public Alpha: number;
		/**读取飞机配置 */
		public getConfig() {
			
			
             //敌机血量每波增加	
			this.hpDef = GameUtil.PlayerMessage.createMsg().planeConfig[this.type].hp +( Math.floor((GameUtil.PlayerMessage.instance.game_turns-1)/ 2));
             //敌机速度每波增加
			this.speed = GameUtil.PlayerMessage.instance.planeConfig[this.type].speed + GameUtil.PlayerMessage.instance.game_turns ;
			this.speed_config=this.speed;
		}
		/**飞机的创建 */
		public create() {
			this.createFlyTexture();
			this.createBlood();
		}
		/**创建飞机纹理 */
		public createFlyTexture() {
			if (this.fly == null) {
				this.fly = new egret.Bitmap();
			}
			this.fly.texture = RES.getRes(EnemyType[this.type]);
			//this.fly.alpha=1;
			if (EnemyType[this.type] == "feiji1_png" || EnemyType[this.type] == "feiji2_png" || EnemyType[this.type] == "feiji3_png") {
				this.fly.width = 80;
				this.fly.height = 83;
			}

		}
		/**飞机血量条的创建 */
		public createBlood() {
			this.hp = this.hpDef;
			if (this.progress == null) {
				this.progress = new eui.ProgressBar();
				this.progress.skinName="resource/assets/FlyM/hpPlane_classics.exml";
				this.progress.width = this.fly.width;
				//this.progress.height = 20;
				this.progress.minimum = 0;
			}
			this.progress.maximum = this.hpDef;
			this.progress.value = this.hpDef;
		}
		/**确定飞机出生位置、id */
		public born(point: egret.Point, tid) {
			this.fly.x = point.x;
			this.fly.y = point.y;
			this.progress.x = this.fly.x + this.fly.width / 2;
			this.progress.y = this.fly.y - this.fly.height / 3;
			this.id = tid;
			this.showPanle.addChild(this.fly);
			this.showPanle.addChild(this.progress);
			this.progress.visible = true;
			this.fly.visible = true;
			this.isLiving = true;
		}
		/**飞机销毁 */
		public destroy() {
			this.flyManager.destoryOneFly(this.id);

			if (this.progress.parent) {
				//this.progress.alpha=0;
				this.progress.parent.removeChild(this.progress);
			}
			if (this.fly.parent) {
				//this.fly.alpha=0;
				this.fly.parent.removeChild(this.fly);
			}
			if (GameUtil.PlayerMessage.instance.isFight) {
				GameUtil.PlayerMessage.instance.AllPlaneCount--;
			}
		}
		/**飞机飞出舞台 */
		public overLine() {
			this.isLiving = false;
			this.progress.visible = false;
			this.fly.visible = false;
		}
		/**飞机每帧运行*/
		public update(deltaTimeStamp) {

			this.move(deltaTimeStamp);
			if (this.fly.y > 1136) {
				this.overLine();
			}
			if (this.hp <= 0) {
				//添加金币跳动
				var point: egret.Point = new egret.Point(this.fly.x, this.fly.y)
				if (this.showPanle.name == "AirBattle") {
					this.AnimationManager.createDeleteAnimation(point, 2);
					this.AnimationManager.createDeleteAnimation(point, 1);
					this.GoldJump.goldJump(this.type);
				}
				else {
					this.AnimationManager.createDeleteAnimation(point, 1);
				}
				this.destroy();
			}
			this.hittedByBullet();
		}
		/**飞机血条跟随移动 */
		protected moveProgress() {
			this.progress.x = this.fly.x;
			this.progress.y = this.fly.y - this.fly.height / 3;
			this.progress.value = this.hp;
		}
		/**飞机移动 */
		public move(deltaTimeStep: number) {
			if (this.showPanle.name == "AirBattle") {

				this.fly.y += this.speed * deltaTimeStep / 15;//匀速飞行
				//this.fly.y+=this.speed*deltaTime/1000;//加速飞行
			}
			else {
				this.flyToPoint(GameUtil.PlayerMessage.createMsg().HomePoint);
			}
			this.moveProgress();
		}
		/**飞机飞向目标点的飞行方式 */
		protected flyToPoint(targetPoint: egret.Point) {
			egret.Tween.removeTweens(this);
			if (egret.Point.distance(targetPoint, new egret.Point(this.fly.x, this.fly.y)) < 20) {
				this.destroy();
				if (GameUtil.PlayerMessage.instance.onePerson) {
					if (this.showPanle.name == "Defense") {
						GameUtil.PlayerMessage.instance.BaseHp--;
					}
					else if(this.showPanle.name == "EnemyDefense"){
						GameUtil.PlayerMessage.instance.enemyHomeHp--;
					}else{

					}
				}
				else {
					if (this.showPanle.name == "Defense") {
						GameUtil.PlayerMessage.instance.BaseHp--;
					}
				}
				return;
			}
			var target = {
				x: targetPoint.x - this.fly.x,
				y: targetPoint.y - this.fly.y
			};
			var dist = Math.sqrt(Math.pow(target.y, 2) + Math.pow(target.x, 2));
			var deg = Math.atan2(target.y, target.x);
			var deltaY = this.speed * Math.sin(deg) * 2;
			var deltaX = this.speed * Math.cos(deg) * 2;
			egret.Tween.get(this).to({ x: this.fly.x += deltaX, y: this.fly.y += deltaY }, 5000);
		}
		/**飞机与子弹的碰撞 */
		protected hittedByBullet() {
			if (this.showPanle.BulletManager) {
				let list: any[] = this.showPanle.BulletManager.productBulletus;
				for (let i = 0; i < list.length; i++) {
					let Tbullet: Bullet.BasicBullet = list[i];
					if (GameHit.GameHit(this.fly, Tbullet.bullet) && list[i].bulletType != 2) {
						this.hp -= Tbullet.bulletATK;
						Tbullet.bulletHp--;
					}
				}
			}
		}

	}
}